package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.CapitalRoomMapper;
import com.yh.homestay.pojo.CapitalRoom;
import com.yh.homestay.service.CapitalRoomService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName CapitalRoomServiceImpl
 * @Description 房间业务逻辑
 * @Author GCL
 * @Date 2020/1/2 10:32
 */
@Service
public class CapitalRoomServiceImpl implements CapitalRoomService {

    private Log logger = LogFactory.getLog(CapitalRoomServiceImpl.class);

    @Autowired
    private CapitalRoomMapper roomMapper;

    /**
     * @param
     * @Date: 2020/1/4 15:12
     * @Description: 查询出所有房间信息
     * @return: com.yh.homestay.common.ResultMessage
     */
    @Override
    public HomeStayResult showCapitalRoom(Integer page, Integer limit, String roomName) {
        try {
            PageHelper.startPage(page, limit);
            List<CapitalRoom> capitalRooms;
            if (StringUtils.isEmpty(roomName)) {
                capitalRooms = roomMapper.selectAll();
            } else {
                Example example = new Example(CapitalRoom.class);
                example.createCriteria().andLike("roomName", "%" + roomName + "%");
                capitalRooms = roomMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(capitalRooms);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }
    /**
     * @param id 房间Id
     * @Date: 2020/1/2 16:45
     * @Description: 删除单个房间信息
     * @return: int
     */
    @Override
    public HomeStayResult deleteCapitalRoom(Integer id) {
        try {
            roomMapper.deleteByPrimaryKey(id);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param ids
     * @Date: 2020/1/2 16:45
     * @Description: 批量删除房间信息
     * @return: int
     */
    @Override
    public HomeStayResult deleteCapitalRoom(String ids) {
        try {
            String[] id = ids.split(",");
            for (int i = 0; i < id.length; i++) {
                roomMapper.deleteByPrimaryKey(id[i]);
            }
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param id
     * @Date: 2020/1/4 17:12
     * @Description: 根据id查询出一条数据
     * @return: com.yh.homestay.common.CapitalRoomResult
     */
    @Override
    public HomeStayResult showOneCapitalRoom(Integer id) {
        try {
            CapitalRoom capitalRoom = roomMapper.selectByPrimaryKey(id);
            return HomeStayResult.ok(200, capitalRoom);
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param room
     * @Date: 2020/1/2 16:46
     * @Description: 编辑信息后更新
     * @return: int
     */
    @Override
    public HomeStayResult updateCapitalRoom(CapitalRoom room) {
        try {
            roomMapper.updateByPrimaryKeySelective(room);
            return HomeStayResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param room
     * @Date: 2020/1/4 15:28
     * @Description: 添加数据到数据库
     * @return: com.yh.homestay.common.CapitalRoomResult
     */
    @Override
    public HomeStayResult insertCapitalRoom(CapitalRoom room) {
        try {
            roomMapper.insertSelective(room);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return HomeStayResult.build(500, "error");
        }
    }

}
